﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormReiseAuxiliaryFrame : System.Windows.Forms.Form
	{
		public event EventHandler lengthAndAngleChanged;

		public FormReiseAuxiliaryFrame()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
		}

		public FormReiseAuxiliaryFrame(bool isHVAC, bool isAuxiliaryFrame) : this()
		{
			this.isHVAC = isHVAC;
			this.isAuxiliaryFrame = isAuxiliaryFrame;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.SetUserData();
			FormReiseDrawVerticalPipe.length = this.hyTextBox1.Text.ToDouble(0.0);
			FormReiseDrawVerticalPipe.angle = this.hyTextBox5.Text.ToDouble(0.0);
			this.lengthAndAngleChanged(sender, new EventArgs());
			base.Close();
		}

		private void FormReiseAuxiliaryFrame_Load(object sender, EventArgs e)
		{
			this.BindPicture();
			this.SetTextEnable(this.isAuxiliaryFrame);
			this.GetUserData();
		}

		private void GetUserData()
		{
			this.hyTextBox1.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameLenght") == "") ? "0" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameLenght"));
			this.hyTextBox5.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameAngle") == "") ? "0" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameAngle"));
		}

		private void SetUserData()
		{
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "AuxiliaryFrameLenght", this.hyTextBox1.Text.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "AuxiliaryFrameAngle", this.hyTextBox5.Text.ToString());
		}

		private void BindPicture()
		{
			if (this.isHVAC)
			{
				if (this.isAuxiliaryFrame)
				{
					this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "AuxiliaryFrameHvac.png"));
					return;
				}
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "NoAuxiliaryFrameHvac.png"));
				return;
			}
			else
			{
				if (this.isAuxiliaryFrame)
				{
					this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "AuxiliaryFrame.png"));
					return;
				}
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "NoAuxiliaryFrame.png"));
				return;
			}
		}

		private void SetTextEnable(bool bIsEnable)
		{
			this.hyTextBox1.Enabled = bIsEnable;
			this.hyTextBox5.Enabled = bIsEnable;
		}

		private void hyTextBox5_TextChanged(object sender, EventArgs e)
		{
			double num = Convert.ToDouble(this.hyTextBox5.Text);
			if (num < 0.0 || num >= 90.0)
			{
				YJKMessageBox.Warning("辅助框行向角的范围为0-90度，且不能等于90度！");
				this.hyTextBox5.Focus();
				this.hyTextBox5.SelectAll();
				this.hyTextBox5.Text = "0";
				return;
			}
		}

		private void hyTextBox5_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)sender;
			if (!Regex.IsMatch(e.KeyChar.ToString(), "^[0-9\\b]+$"))
			{
				YJKMessageBox.Warning("文本框请输入数字！");
				textBox.Focus();
				e.Handled = true;
			}
		}

		private void hyTextBox5_Validating(object sender, CancelEventArgs e)
		{
			if (!Regex.IsMatch(this.hyTextBox5.Text, "^[0-9]+$"))
			{
				YJKMessageBox.Warning("文本框请输入数字！");
				this.hyTextBox5.Focus();
				this.hyTextBox5.SelectAll();
				return;
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		private bool isHVAC;

		private bool isAuxiliaryFrame;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private string imageFilePath = Product.ResLocation;
	}
}
